package asionreachel.cn.smarthomedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import familyclient.android.configer.JSmartConfiger;

/**
 * Created by SuperMa on 2017/6/26.
 */

public class OneKeyActivity extends BaseActivity {
    private EditText m_etPassword;
    private TextView m_tvAccount = null;
    private JSmartConfiger m_configerSmart = new JSmartConfiger();

    //一键配置请求返回监听
    private JSmartConfiger.JSmartConfigerListener m_listenerSmartConfiger = new JSmartConfiger.JSmartConfigerListener() {
        @Override
        public void DeviceJoinedCb() {
            // One device joined
            Common.setDialogLoadingMsg( "已配置" + m_configerSmart.GetJoinedCount() + "个设备" );
        }

        @Override
        public void ConfigFinishedCb( int a_iJoinedCount ) {
            // Finish config
            Common.closeDialogLoading();
            if( a_iJoinedCount > 0 ) {
                Common.showToast("配置完成:"+a_iJoinedCount);
                setResult( RESULT_OK, null );
                finish();
            } else {
                Common.showToast("配置失败，无设备加入");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView( R.layout.activity_one_key );

        m_configerSmart.SetListener( m_listenerSmartConfiger );

        ImageView ivBack = (ImageView)findViewById( R.id.iv_back );
        m_tvAccount = (TextView)findViewById(R.id.onekey_tv_account);
        m_etPassword = (EditText)findViewById(R.id.onekey_ed_pass);
        TextView tvConnect = (TextView)findViewById(R.id.onekey_tv_connect);
        Button btnNext = (Button)findViewById(R.id.onekey_bt_setting);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });

        //跳转到wifi选择界面
        tvConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
                startActivity(wifiSettingsIntent);
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strWifiSSID = Common.getWifiName();
                String strWifiBSSID = Common.getWifiBSSID();
                String strWifiPwd = m_etPassword.getText().toString().trim();
                if(TextUtils.isEmpty(strWifiSSID)){
                    Common.showToast("请输入Wifi账号");
                    return;
                }if(TextUtils.isEmpty(strWifiPwd)){
                    Common.showToast("请输入Wifi密码");
                    return;
                }
                if(strWifiPwd.length()<8){
                    Common.showToast("Wifi密码格式不正确");
                    return;
                }

                //一键配置接口
                m_configerSmart.StartConfigDevice( strWifiSSID, strWifiBSSID, strWifiPwd, "CDM1_Super", "", 1, OneKeyActivity.this );
                Common.showDialogLoading( OneKeyActivity.this , "正在配置");
            }
        });

        setWifiName();
        CheckBox cbPW = (CheckBox)findViewById(R.id.onekey_cb_pw);
        boolean bCheck = cbPW.isChecked();
        Common.setEditTextStyle(m_etPassword , bCheck);
        cbPW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Common.setEditTextStyle(m_etPassword , isChecked);
            }
        });
    }

    public void onBack() {
        finish();
    }

    @Override
    public void onBackKey() {
        onBack();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setWifiName();
    }

    @Override
    protected void onPause() {
        m_configerSmart.StopConfigDevice();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        m_configerSmart.SetListener( null );
        super.onDestroy();
    }

    private void setWifiName()
    {
        String wifiName = Common.getWifiName();
        if (m_tvAccount != null)
            m_tvAccount.setText( wifiName );
    }
}
